﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 统计据库操作类
    /// </summary>
    public class DaoStatistics : DAO
    {
        /// <summary>
        /// 获取点击数
        /// </summary>
        /// <returns></returns>
        public static DataTable GetCount(int sharedId, int advertId, int userId)
        {
            Database db = GetDatabase();
            DbCommand cmd = db.GetStoredProcCommand("ClickCount");
            db.AddInParameter(cmd, "@shared_id", DbType.Int32, sharedId == 0 ? null : (object)sharedId);
            db.AddInParameter(cmd, "@advert_id", DbType.Int32, advertId == 0 ? null : (object)advertId);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, userId);
            DataSet ds = db.ExecuteDataSet(cmd);
            return ds.Tables[0];
        }

        /// <summary>
        /// 点击操作
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="userAgent"></param>
        /// <param name="sharedId"></param>
        /// <param name="type"></param>
        public static void Click(MSharedClick m, int type, int advert_id, int clickNumber)
        {
            //ip拆分ip
            String[] ips = m.ip.Split('.');

            Int64 ip_val = Int64.Parse(ips[0]) * 256 * 256 * 256 + Int64.Parse(ips[1]) * 256 * 256 + Int64.Parse(ips[2]) * 256 + Int64.Parse(ips[3]);

            Database db = GetDatabase();
            DbCommand cmd = db.GetStoredProcCommand("ClientClick");
            db.AddInParameter(cmd, "@browser", DbType.String, m.browser);
            db.AddInParameter(cmd, "@os", DbType.String, m.os);
            db.AddInParameter(cmd, "@advert_id", DbType.Int32, advert_id);
            db.AddInParameter(cmd, "@shared_id", DbType.Int32, m.shared_id);
            db.AddInParameter(cmd, "@type", DbType.Int32, type);
            db.AddInParameter(cmd, "@ip_val", DbType.Int64, ip_val);
            db.AddInParameter(cmd, "@number", DbType.Int32, clickNumber);
            db.ExecuteNonQuery(cmd);
        }
    }
}
